/*
 * Copyright (c) 2006 Jordan Bayliss-McCulloch
 * Email: j.baylissmcculloch@gmail.com
 */

import java.awt.*;
import java.awt.image.*;

public class FloodFill {
	BufferedImage buffer;

	public FloodFill( BufferedImage buffer) {
		this.buffer = buffer;
	}

	private int makeRGB(int r, int g, int b) {
		return 255 << 24 | r << 16 | g << 8 | b;
	}  
	
	/* very inefficient 2-level iteration */
	public void replaceColor( Color find, Color replace) {
		final int w=buffer.getWidth();
		final int h=buffer.getHeight();
		final int f = makeRGB( find.getRed(), find.getGreen(), find.getBlue() );
		final int r = makeRGB( replace.getRed(), replace.getGreen(), replace.getBlue() );

		for (int x=0; x<w; x++)
			for (int y=0; y<h;y++) {
				if (buffer.getRGB(x,y) == f) {
					buffer.setRGB(x,y,r);
				}
			}
		}
	}
}
